<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/8/27
 * Time: 10:01
 */

namespace app\merchant\controller;


use app\common\controller\ActionLogs;
use app\common\model\Agent;
use app\common\model\AgentSales;
use app\common\model\AgentSys;
use app\common\model\CardGeneral;
use app\common\model\CardGift;
use app\common\model\CardMember;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use think\Config;
use think\Session;

class CardApiBase
{

    public function getagentlist()
    {

        $Agent = new Agent();
        if (Session::get('user.uid') == 1) {
            $map = [];
        } else {
            $map['sysid'] = Session::get('sysloginid');
        }
        $list = $Agent->where($map)->field('id')->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $id[] = $v['id'];
            }
            $sid = implode(',', $id);
        } else {
            $sid = "";
        }
        return $sid;

    }

    public function get_agent_id()
    {
        $map['uid'] = Session::get('user.uid');
        $agentid = Agent::where($map)->value('id');
        return $agentid;


    }

    public function get_mer_mid()
    {
        $level2 = Session::get('level2');
        if (empty($level2)) {
            $LoginUsers = new LoginUsers();
            $login_mid = Session::get('login_mid');
            $login_top_mid = Session::get('login_top_mid');
            $user = $LoginUsers->where(['id' => Session::get('user.uid')])->find();
            if ($user['level'] == '3001') {
                $Merchant = new Merchant();
                $midlist = $Merchant->where(['top_mid' => $login_top_mid])->field('id')->select();
                $smid = [];
                foreach ($midlist as $k => $v) {
                    $smid[] = $v['id'];
                }
                $mid = implode(',', $smid);
            } elseif ($user['level'] == '3002' || $user['level'] == '3003' || $user['level'] == '3004') {
                $mid = $LoginUsers->where(['id' => Session::get('user.uid')])->value('mid');
            } elseif ($user['level'] == '2001') {
                $Agent = new Agent();
                $mas['agent'] = $Agent->where('uid', $user['id'])->value('id');
                $Merchant = new Merchant();
                $mids = $Merchant->where($mas)->field('id')->select();
                $smid = [];
                foreach ($mids as $k => $v) {
                    $smid[] = $v['id'];
                }
                $mid = implode(',', $smid);
            } elseif ($user['level'] == '2002') {
                $AgentSales = new AgentSales();
                $mas['salesman'] = $AgentSales->where('uid', $user['id'])->value('id');
                $Merchant = new Merchant();
                $mids = $Merchant->where($mas)->field('id')->select();
                $smid = [];
                foreach ($mids as $k => $v) {
                    $smid[] = $v['id'];
                }
                $mid = implode(',', $smid);
            }
        } else {
            $Merchant = new Merchant();
            $midlist = $Merchant->where(['top_mid' => Session::get('login_top_mid')])->field('id')->select();
            $smid = [];
            foreach ($midlist as $k => $v) {
                $smid[] = $v['id'];
            }
            $mid = implode(',', $smid);
        }
        return $mid;
    }

    //获取门店数量
    public function get_mid_num()
    {
        $mid = $this->get_mer_mid();
        $mids = explode(',', $mid);
        $num = count($mids);
        return $num;
    }

    public function get_qx_mid($mid)
    {
        $smid = $this->get_mer_mid();
        $arr_mid = explode(',', $smid);
        $isin = in_array($mid, $arr_mid);
        if ($isin) {
            return true;
        } else {
            return false;
        }


    }

    public function getagent($mid = "", $agentid = "")
    {
        if ($agentid) {
            $agent = $agentid;

        } else {
            if ($mid) {
                $Merchant = new Merchant();
                $agent = $Merchant->where('id', $mid)->value('agent');

            } else {
                $top_mid = Session::get('login_top_mid');
                $Momerchant = new Momerchant();
                $agent = $Momerchant->where('id', $top_mid)->value('agent');

            }
        }
        return $agent;
    }

    public function getcopyright($agentid)
    {

        $map['id'] = $agentid;
        $agent = new Agent();
        $info = $agent->where($map)->field('cpsysname,copyright,cpurl,sysid,client_bs')->find();
        $ActionLogs = new ActionLogs();
        $ActionLogs->writelog("payos", "login", "check_agent/cop", "2:", $info);
        if (!empty($info['cpsysname']) && !empty($info['copyright']) && !empty($info['cpurl'])) {
            $wx['cpsysname'] = $info['cpsysname'];
            $wx['copyright'] = $info['copyright'];
            $wx['cpurl'] = $info['cpurl'];

        } else {

            $mab['id'] = $info['sysid'];
            $AgentSys = new AgentSys();
            $info2 = $AgentSys->where($mab)->field('cpsysname,copyright,cpurl')->find();
            if (empty($info2['cpsysname']) && empty($info2['copyright']) && empty($info2['cpurl'])) {

                $wx['cpsysname'] = Config::get('webconfig.copyright_brand');
                $wx['copyright'] = Config::get('webconfig.copyright_name');
                $wx['cpurl'] = Config::get('webconfig.copyright_url');
            } else {

                $wx['cpsysname'] = $info2['cpsysname'];
                $wx['copyright'] = $info2['copyright'];
                $wx['cpurl'] = $info2['cpurl'];
            }

        }
        if ($info['client_bs']) {
            $wx['client_bs'] = $info['client_bs'] . "/";
        } else {
            $wx['client_bs'] = "";
        }
        $ActionLogs = new ActionLogs();
        $ActionLogs->writelog("payos", "login", "check_agent/cop", "3:", $wx);
        Session::set('cp.cpsysname', $wx['cpsysname']);
        Session::set('cp.copyright', $wx['copyright']);
        Session::set('cp.cpurl', $wx['cpurl']);
        Session::set('cp.client_bs', $wx['client_bs']);

    }

    public function getcopyrightsys($sysid)
    {

        $map['id'] = $sysid;
        $agent = new AgentSys();
        $info = $agent->where($map)->field('cpsysname,copyright,cpurl')->find();

//        return $info->getLastSql();
        if (empty($info['cpsysname']) && empty($info['copyright']) && empty($info['cpurl'])) {
            $wx['cpsysname'] = Config::get('webconfig.copyright_brand');
            $wx['copyright'] = Config::get('webconfig.copyright_name');
            $wx['cpurl'] = Config::get('webconfig.copyright_url');
        } else {

            $wx['cpsysname'] = $info['cpsysname'];
            $wx['copyright'] = $info['copyright'];
            $wx['cpurl'] = $info['cpurl'];
        }
        Session::set('cp.cpsysname', $wx['cpsysname']);
        Session::set('cp.copyright', $wx['copyright']);
        Session::set('cp.cpurl', $wx['cpurl']);


    }

    //获取授权状态
    public function get_auth_type($mid = '')
    {
//        if (empty($mid)){
//            $Momerchant = new Momerchant();
//            $top_mid = Session::get('login_top_mid');
//            $wx_open_id = $Momerchant->where('id', $top_mid)->value('wx_open_id');
//            if ($wx_open_id){
//                $res = 1;
//            }else{
//                $res = 0;
//            }
//        }else {
        if ($mid) {
            $Merchant = new Merchant();
            $wx_open_id = $Merchant->where('id', $mid)->value('wx_open_id');

        } else {
            $top_mid = Session::get('login_top_mid');
            $Momerchant = new Momerchant();
            $wx_open_id = $Momerchant->where('id', $top_mid)->value('wx_open_id');
//           return $wx_open_id;
        }
//            if ($wx_open_id){
//                $res = 1;
//            }else{
//                $res = 0;
//            }
//        }

        return $wx_open_id;
    }

    //获取card_id
    public function get_card_id(){
        $CardMerchant = new CardMember();
        $card_id = $CardMerchant->where(['top_mid'=>Session::get('login_top_mid')])->value('card_id');
        return $card_id;
    }

    //门店列表
    public function get_machlist(){
        //门店
        $Merchant = new Merchant();
        $mab['id'] = ['in',$this->get_mer_mid()];
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach',$mach);
    }

    //检查mid 是否数据top_mid
    public function checkmid($mid)
    {
        $Merchant = new Merchant();
        $map['id'] = $mid;
        $map['top_mid'] = Session::get('login_top_mid');
        $is = $Merchant->where($map)->value('id');
        if ($is) {
            $res = true;
        } else {
            $res = false;
        }
        return $res;
    }

    //检查mid 是否数据top_mid
    public function checkcardid($card_id)
    {
        $CardGeneral = new CardGeneral();
        $CardMember = new CardMember();
        $CardGift = new CardGift();
        $map['card_id'] = $card_id;
        $map['top_mid'] = Session::get('login_top_mid');
        $is = $CardGeneral->where($map)->value('id');
        if ($is) {
            $res = true;
        } else {
            $is = $CardMember->where($map)->value('id');
            if ($is) {
                $res = true;
            } else {
                $is = $CardGift->where($map)->value('id');
                if ($is){
                    $res = true;
                }else {
                    $res = false;
                }
            }
        }
        return $res;
    }

}